package com.cloud.invoker.controller;

import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.cloud.invoker.entity.Person;

@RestController
@Configuration
public class InvokerController {

	private static final String REST_URL_PREFIX = "http://PROVIDER-SOME/";

	@Bean
	@LoadBalanced
	public RestTemplate getRestTemplate() {
		return new RestTemplate();
	}

	@PostMapping(value = "/post", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
	public Person get(@RequestBody Person person) {
		RestTemplate restTemplate = getRestTemplate();
		Person result = restTemplate.postForEntity(REST_URL_PREFIX + "findPerson", person, Person.class).getBody();
		return result;
	}
}
